package com.xianbeiying.app.ckgl.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.bean.ResetBean;
import com.xianbeiying.app.ckgl.http.HttpCallBackUtil;
import com.xianbeiying.app.ckgl.model.SetModel;
import com.xianbeiying.app.ckgl.util.SharedPreferencesUtils;
import com.xianbeiying.app.ckgl.util.ToastUtil;

/**
 * 重置密码
 * */
public class ResetPasswordActivity extends BaseActivity  implements View.OnClickListener,HttpCallBackUtil{
    private ImageView iv_title_left;
    private TextView tv_title;
    private EditText et_arest_pwd,et_rest_pwd;
    private Button rest_btn;
    private TextView tv_pwd1,tv_pwd2;
    private SetModel setModel;
    private String username;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_password);
        initView();
    }
    private void initView() {
        setModel=new SetModel(this);
        setModel.addResponseListener(this);
        username=getIntent().getStringExtra("username");
        iv_title_left = (ImageView) findViewById(R.id.iv_title_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_pwd1=(TextView) findViewById(R.id.tv_pwd1);
        tv_pwd2=(TextView) findViewById(R.id.tv_pwd2);
        et_rest_pwd=(EditText) findViewById(R.id.et_rest_pwd);
        et_arest_pwd=(EditText) findViewById(R.id.et_arest_pwd);
        rest_btn=(Button) findViewById(R.id.rest_btn);
        tv_title.setText("重置密码");
        tv_pwd1.setText("新密码");
        tv_pwd2.setText("确认新密码");
        iv_title_left.setOnClickListener(this);
        rest_btn.setOnClickListener(this);

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rest_btn:
              String pwd=et_rest_pwd.getText().toString().trim();
              String rpwd=et_arest_pwd.getText().toString().trim();
                if(pwd.isEmpty()){
                    ToastUtil.showInfo(this,"密码不能为空");
                    return;
                }
                if(rpwd.isEmpty()){
                    ToastUtil.showInfo(this,"密码不能为空");
                    return;
                }
                if(!pwd.equals(rpwd)){
                    ToastUtil.showInfo(this,"两次输入密码不一致");
                    return;
                }
                setModel.resetPwd(username,pwd,true);
                break;

            case R.id.iv_title_left:
                    finish();
                break;
        }
    }

    private ResetBean resetBean;
    @Override
    public void onCallBackData(String url,Object content) {
        if(url.equals("重置密码")){
            resetBean= JSON.parseObject(new String((byte[]) content), ResetBean.class);
            if(resetBean.isR()){
                ToastUtil.showInfo(ResetPasswordActivity.this,"修改成功");
                SharedPreferencesUtils.setStringDate("username", "");
                SharedPreferencesUtils.setStringDate("userpwd", "");
                finish();
            }else{
                ToastUtil.showInfo(ResetPasswordActivity.this,resetBean.getERR_MSG());
            }
        }

    }
}
